<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->

    <title>sign up</title>
    <link rel="stylesheet" href="../static/assets/sweetalert2/sweetalert2.min.css"
          th:href="@{/assets/sweetalert2/sweetalert2.min.css}"/>
    <link rel="stylesheet" href="../static/assets/bootstrap/css/bootstrap.min.css"
          th:href="@{/assets/bootstrap/css/bootstrap.min.css}"/>
    <link rel="stylesheet"
          href="../static/assets/tianai/styles/tac.css"
          th:href="@{/assets/tianai/styles/tac.css}" />
</head>

<body>
<section>
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-sm-3">
                <form id="signUpForm" action="/signUp" method="post">
                    <div class="form-group row my-2 px-2 justify-content-center">
                        <input type="hidden" name="graphId" id="graphId" />
                        <h3 class="text-center">注册</h3>
                        <p class="text-center alert alert-danger" th:if="${error!=null && error!=''}" th:text="${error}"></p>
                    </div>

                    <div class="form-group row my-2 px-2 justify-content-center">
                        <input type="text" name="username" class="form-control" id="username" required="required"
                               placeholder="用户名"/>
                    </div>
                    <div class="form-group row my-2 px-2 justify-content-center">
                        <input type="password" name="password" class="form-control" id="password"
                               required="required" placeholder="密　码"/>
                    </div>
                    <div class="form-group row my-2 px-2 justify-content-center">
                        <input type="password" name="confirmPassword" class="form-control" id="confirmPassword"
                               required="required" placeholder="确认密码"/>
                    </div>
                    <div id="captcha-box"></div>
                    <div class="form-group row my-2 px-2 justify-content-center">
                        <button type="submit" class="btn btn-primary btn-block">注册</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>
<script src="../static/assets/jquery.min.js"
        th:src="@{/assets/jquery.min.js}"></script>
<script src="../static/assets/bootstrap/js/bootstrap.min.js"
        th:src="@{/assets/bootstrap/js/bootstrap.min.js}"></script>
<script th:src="@{/assets/localforage.min.js}" src="../static/assets/localforage.min.js"></script>
<script th:src="@{/assets/sweetalert2/sweetalert2.all.min.js}"
        src="../static/assets/sweetalert2/sweetalert2.all.min.js"></script>
<script src="../static//assets/tianai/tac.min.js"
        th:src="@{/assets/tianai/tac.min.js}"></script>
<script th:inline="javascript">
    /*<![CDATA[*/
    var captchaPassed = false;
    $(document).ready(function () {
        const config = {
           // 生成接口 (必选项,必须配置, 要符合tianai-captcha默认验证码生成接口规范) 参考 https://gitee.com/tianai/tianai-captcha-demo
           requestCaptchaDataUrl: "/captcha/gen",
           // 验证接口 (必选项,必须配置, 要符合tianai-captcha默认验证码校验接口规范) 参考 https://gitee.com/tianai/tianai-captcha-demo
           validCaptchaUrl: "/captcha/check",
           // 验证码绑定的div块 (必选项,必须配置)
           bindEl: "#captcha-box",
           // 验证成功回调函数(必选项,必须配置)
           validSuccess: (res, c, tac) => {
               // todo 这里res为后端返回的数据，一般返回验证成功的token， 进行登录或其它操作时，要把返回的token传给后端进行二次验证
               console.log("验证成功，后端返回的数据为", res);
              // 调用登录方法进行登录 这里login登录方法为自己的登录函数
              //   this.login(res.captchaToken)
             captchaPassed = true;
             $("#signUpForm").submit();
               // 销毁验证码服务
             tac.destroyWindow();
           },
           // 验证失败的回调函数(可忽略，如果不自定义 validFail 方法时，会使用默认的)
           validFail: (res, c, tac) => {
               // 验证失败后重新拉取验证码
               tac.reloadCaptcha();
           }
        }

        function trimBlank(str) {
            return str.replace(/(^\s*)|(\s*$)/g, '')
        }

        $("#signUpForm").on("submit", function (e) {

            if ($("#username").val() == null || trimBlank($("#username").val()) == '') {
                $("#username").focus();
                e.preventDefault();
                return false;
            }

            if ($("#password").val() == null || $("#password").val().trim() == '') {
                $("#password").focus();
                e.preventDefault();
                return false;
            }

            if ($("#confirmPassword").val() == null || $("#confirmPassword").val().trim() == '') {
                $("#confirmPassword").focus();
                e.preventDefault();
                return false;
            }

            if ($("#confirmPassword").val() != $("#password").val()) {
                $("#confirmPassword").focus();
                e.preventDefault();
                return false;
            }

            if(captchaPassed == false){
                event.preventDefault();
                // 创建 TAC 启动验证码服务,调用该方法后会在指定的div块中渲染出验证码
                new TAC(config).init();
                $(this).parents("#signUpForm").submit();
            }else{
                $.ajax({
                    url: "/signUp",
                    method: 'post',
                    data: $('#signUpForm').serialize(),
                    success: function (result) {
                        if (result.status < 0) {
                            Swal.fire({
                              title: 'Oops...',
                              text: result.message,
                              icon: 'error',
                              confirmButtonText: '确定'
                            })
                        } else {
                            window.location.href = "/";
                        }
                    }
                })
            }
            return false;
        })
    });
    /*]]>*/
</script>

</body>
</html>
